﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Dynamic;
using Newtonsoft.Json.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
namespace Common
{
    public class JsonHelper
    {
        /// <summary>
        /// json序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string Serialize<T>(T t) where T:class
        {
            if (t == null)
            {
                return "";
            }
            return JsonConvert.SerializeObject(t);
        }
        /// <summary>
        /// json反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="src">json字符串</param>
        /// <returns></returns>
        public static T Deserialize<T>(string src) where T:class
        {
            if (string.IsNullOrEmpty(src))
            {
                return null;
            }
            
            return JsonConvert.DeserializeObject<T>(src);
        }
        /// <summary>
        /// 动态解析json字符串
        /// </summary>
        /// <param name="src">json字符串</param>
        /// <returns></returns>
        public static dynamic Deserialize(string src)
        {
            if (string.IsNullOrEmpty(src))
            {
                return null;
            }
            dynamic jsonObject = null;
            try
            {
                  jsonObject = JObject.Parse(src);
            }
            catch (Exception )
            {
                
                throw;
            }
            return jsonObject;
        }
        /// <summary>
        /// 将Json数组转换为对象集合——————不建议使用
        /// 如果数据类型转换出错可参考文章http://blog.csdn.net/xiaohan2826/article/details/8536074对泛型的处理
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="src"></param>
        /// <returns></returns>
        public static IList<T> ArryJosn2List<T>(string src) where T:class,new()
        {
            if (string.IsNullOrEmpty(src))
            {
                return null;
            }
            JArray jsonarr = JArray.Parse(src);
            PropertyInfo [] props=typeof(T).GetProperties();
            IList<T> list = jsonarr.Select(x => {
                T t=new T();
                for (int i = 0; i < props.Length; i++)
                {
                    //var pi=props[i];
                    //pi.SetValue(t, Convert.ChangeType(x[pi.Name], pi.PropertyType), null);
                    props[i].SetValue(t, Convert.ChangeType(x[props[i].Name], props[i].PropertyType), null);  
                    
                }
                return t;
            }).ToList();
            return list;
        }
        /// <summary>
        /// json转为xml
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static XmlDocument  Json2Xml(string src)
        {
            if (string.IsNullOrEmpty(src))
            {
                return null;
            }
           return JsonConvert.DeserializeXmlNode(src);
        }
        /// <summary>
        /// json转为linq xml
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static XDocument Json2LinqXml(string src)
        {
            if (string.IsNullOrEmpty(src))
            {
                return null;
            }
            return JsonConvert.DeserializeXNode(src);
            
        }
    }
}
